package me.ywy.apt.msgimport.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.Serializable;
import java.util.List;

import me.ywy.apt.msgimport.R;
import me.ywy.apt.msgimport.bean.MsgEntity;
import me.ywy.apt.msgimport.detail.DetailActivity;
import me.ywy.apt.msgimport.utils.ToastUtils;

public class MainActivity extends AppCompatActivity implements MainContract.View, View.OnClickListener {


    private MainContract.Presenter mPresenter;
    private MsgAdapter mAdapter;
    private RecyclerView mainRecyclerView;
    private EditText textInput;
    private Button mainConvert;
    private Button mainBuild;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mainRecyclerView.setLayoutManager(linearLayoutManager);

        initData();
    }

    private void initData() {
        mPresenter = new MainPresenter(this, this);
        mAdapter = new MsgAdapter(mPresenter.getData());
        mainRecyclerView.setAdapter(mAdapter);
    }

    private void buildClick() {
        ToastUtils.showToastS(this, "main_build");
    }

    private void convertClick() {
        String inputMsg = textInput.getText().toString().trim();
        if(TextUtils.isEmpty(inputMsg)){
            return;
        }
        mPresenter.imputMsg(inputMsg);
        ToastUtils.showToastS(this, "main_convert");
    }


    @Override
    public void viewRefreach() {
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void viewRefreachRemove(int position) {
        mAdapter.notifyItemRemoved(position);
    }

    @Override
    public void viewRefreachItem(int position) {
        mAdapter.notifyItemChanged(position);
    }

    @Override
    public void showError(String s) {
        ToastUtils.showToastS(this , s);
    }

    @Override
    public void convertSuccess() {
        textInput.setText("");
    }

    private void initView() {
        mainRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_view);
        textInput = (EditText) findViewById(R.id.text_input);
        mainConvert = (Button) findViewById(R.id.main_convert);
        mainBuild = (Button) findViewById(R.id.main_build);

        mainConvert.setOnClickListener(this);
        mainBuild.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_convert:
                convertClick();
                break;
            case R.id.main_build:
                buildClick();
                break;
        }
    }

    class VH extends RecyclerView.ViewHolder {
        public final TextView itemTel;
        public final TextView itemName;
        public final TextView itemAddress;
        public final TextView itemDesc;
        public final Button itemMsgDel;
        public final Button itemMsgChange;

        public VH(View itemView) {
            super(itemView);
            itemName = itemView.findViewById(R.id.item_name);
            itemTel = itemView.findViewById(R.id.item_tel);
            itemAddress = itemView.findViewById(R.id.item_address);
            itemDesc = itemView.findViewById(R.id.item_desc);
            itemMsgDel = itemView.findViewById(R.id.item_msg_delete);
            itemMsgChange = itemView.findViewById(R.id.item_msg_change);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == DetailActivity.DETAIL_RESULTCODE){
            Bundle bundle = data.getExtras();
            MsgEntity entity = (MsgEntity) bundle.getSerializable(DetailActivity.MSG_KEY);
            int position = bundle.getInt(DetailActivity.POSITION_KEY);
            mPresenter.updateDataListItem(entity , position);
        }
    }

    class MsgAdapter extends RecyclerView.Adapter<VH> {

        private List<MsgEntity> dataList;

        public MsgAdapter(List<MsgEntity> dataList) {
            this.dataList = dataList;
        }

        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_msg, parent, false);
            VH viewholder = new VH(inflate);
            return viewholder;
        }

        @Override
        public void onBindViewHolder(@NonNull VH holder, final int position) {
            final MsgEntity msgEntity = dataList.get(position);
            holder.itemName.setText(msgEntity.getName());
            holder.itemTel.setText(msgEntity.getPhone());
            holder.itemAddress.setText(msgEntity.getAddress());
            holder.itemDesc.setText(msgEntity.getDesc());
            holder.itemMsgDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.removeItem(position);
                }
            });
            holder.itemMsgChange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPresenter.detailEdit(msgEntity , position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return dataList == null ? 0 : dataList.size();
        }
    }
}
